package cn.yto.print.sqlite.service.impl;

import cn.yto.print.enums.PrintStatusEnum;
import cn.yto.print.model.dto.PrintHistoryDTO;
import cn.yto.print.model.request.HistoryQueryRequest;
import cn.yto.print.model.response.PageResponse;
import cn.yto.print.sqlite.dao.PrintHistoryMapper;
import cn.yto.print.sqlite.domain.PrintHistoryDO;
import cn.yto.print.sqlite.service.IPrintHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class PrintHistoryServiceImpl implements DisposableBean, IPrintHistoryService {

    @Autowired
    private PrintHistoryMapper printHistoryMapper;

    @Override
    public void batchSaveHistory(List<PrintHistoryDO> printHistoryList) {
        if (printHistoryList == null || printHistoryList.isEmpty()) {
            return;
        }
        try {
            printHistoryMapper.batchInsert(printHistoryList);
        } catch (Exception e) {
            log.error("保存打印记录发生异常", e);
        }
    }

    @Override
    public PageResponse<PrintHistoryDTO> queryByPage(HistoryQueryRequest queryRequest, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        // 查询总条数
        long total = printHistoryMapper.selectCount(queryRequest);
        if (total == 0) {
            return new PageResponse<>(0, Collections.emptyList(), pageNum, pageSize);
        }
        List<PrintHistoryDTO> list = printHistoryMapper.selectByPage(queryRequest, offset, pageSize);
        list.forEach(item -> {
            item.setPrintStatusText(PrintStatusEnum.getStatusByStatus(item.getPrintStatus()).getDesc());
        });
        return new PageResponse<>(total, list, pageNum, pageSize);
    }

    @Override
    public void updatePrintHistory(List<String> documentIds, Integer printStatus) {
        if (documentIds == null || documentIds.isEmpty()) {
            return;
        }
        try {
            printHistoryMapper.updatePrintStatus(documentIds, printStatus);
        } catch (Exception e) {
            log.error("更新打印记录发生异常", e);
        }
    }

    @Override
    public void clearPrintHistory() {
        printHistoryMapper.clearPrintHistory();
    }

    @Override
    @Transactional
    public void destroy() {
        LocalDateTime expireTime = LocalDateTime.now().minusDays(30).withHour(0).withMinute(0).withSecond(0).withNano(0);
        String expireTimeStr = expireTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        printHistoryMapper.deleteByCondition(expireTimeStr);
    }

    @Transactional
    public boolean deleteSingle(Long id) {
        return printHistoryMapper.deleteById(id) > 0;
    }

    @Transactional
    public int deleteBatch(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return 0;
        }
        return printHistoryMapper.deleteBatch(ids);
    }

}
